package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.AgentiaVerifyRecordMapper;
import com.zhijian.medical.entity.po.Agentia;
import com.zhijian.medical.entity.po.AgentiaVerifyRecord;
import com.zhijian.medical.entity.vo.request.AgentiaVerifyRecordSearchVo;
import com.zhijian.medical.entity.vo.response.AgentiaVerifyRecordVo;
import com.zhijian.medical.enums.ResponseCodeEnum;
import com.zhijian.medical.exception.BusinessException;
import com.zhijian.medical.service.AgentiaService;
import com.zhijian.medical.service.AgentiaVerifyRecordService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 试剂验证记录业务处理层
 *
 * @author HCG
 * @date 2022-01-15 21:57:36
 * @version: 1.0
 */
@Slf4j
@Service
public class AgentiaVerifyRecordServiceImpl implements AgentiaVerifyRecordService {

    @Autowired
    private AgentiaVerifyRecordMapper mapper;

    @Autowired
    private AgentiaService agentiaService;

    @Override
    public AgentiaVerifyRecord selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public AgentiaVerifyRecordVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(AgentiaVerifyRecord agentiaVerifyRecord) {
        Date now = new Date();
        agentiaVerifyRecord.setCreateTime(now);
        agentiaVerifyRecord.setUpdateTime(now);
        return mapper.insert(agentiaVerifyRecord) == 1 ? agentiaVerifyRecord.getId() : 0L;
    }

    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    @Override
    public Long insertAll(AgentiaVerifyRecord agentiaVerifyRecord) {
        Integer count = this.count(AgentiaVerifyRecordSearchVo.builder()
                .agentiaId(agentiaVerifyRecord.getAgentiaId())
                .build());
        if (count > 0) {
            throw new BusinessException(ResponseCodeEnum.FAILURE.code, "一批次试剂只允许一次验证");
        }
        Long id = this.insert(agentiaVerifyRecord);
        //验收结论
        String conclusion = agentiaVerifyRecord.getConclusion();
        agentiaService.updateByPrimaryKeySelective(Agentia.builder()
                .id(agentiaVerifyRecord.getAgentiaId())
                .status("合格".equals(conclusion) ? (byte) 0 : (byte) 1)
                .build());
        return id;
    }

    @Override
    public boolean update(AgentiaVerifyRecord agentiaVerifyRecord) {
        Date now = new Date();
        agentiaVerifyRecord.setUpdateTime(now);
        return mapper.updateByPrimaryKey(agentiaVerifyRecord) != 0;
    }

    @Override
    public Integer count(AgentiaVerifyRecordSearchVo agentiaVerifyRecordSearchVo) {
        return mapper.count(agentiaVerifyRecordSearchVo);
    }

    @Override
    public List<AgentiaVerifyRecordVo> selectList(AgentiaVerifyRecordSearchVo agentiaVerifyRecordSearchVo) {
        return mapper.selectList(agentiaVerifyRecordSearchVo);
    }

    @Override
    public Page<AgentiaVerifyRecordVo> selectPage(AgentiaVerifyRecordSearchVo agentiaVerifyRecordSearchVo) {
        //先直接查总数
        int count = count(agentiaVerifyRecordSearchVo);
        //分页
        Page<AgentiaVerifyRecordVo> page = new Page<>(true, count, agentiaVerifyRecordSearchVo.getPageNum(), agentiaVerifyRecordSearchVo.getPageSize());
        agentiaVerifyRecordSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<AgentiaVerifyRecordVo> responseVos = this.selectList(agentiaVerifyRecordSearchVo);
        page.setData(responseVos);
        return page;
    }

}